package project.controller;

import project.entity.User;
import project.service.UserService;
import project.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
杨威
2021/9/11
17:56
*/
@WebServlet("/userLogin")
public class LoginController extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");
        String checkNode = req.getParameter("checkNode");
        String node = (String)req.getSession().getAttribute("code");
        if(checkNode.isEmpty() || !(node.equalsIgnoreCase(checkNode))){
            resp.sendRedirect("/day36/login.html");
            return;
        }
        User user = userService.login(userName, passWord);
        if (user != null) {
            req.getSession().setAttribute("user", user);
            resp.sendRedirect(req.getContextPath()+"/showAllStudentController");
        } else {
            resp.sendRedirect("/day36/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
